﻿using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
namespace INTEX.DataBase.Tools
{
    public partial class INTEXDBC
    {
        public DataRow Convert2ABARTE(DataRow dwInput, string AUF, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryABARTE(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (AUF)
            {
                case "YGRAB50MU":
                default:
                    #region====YGRAB50MU======
                    dwReturn["ABEMANDANT"] = "00";
                    dwReturn["ABEAUFKRS"] = "HM";
                    dwReturn["ABEAUFLFD"] = "000004";
                    dwReturn["ABEAUFPOS"] = "001";
                    dwReturn["ABEAFOLGE"] = "020";
                    dwReturn["ABEAEVOR"] = "0001";
                    dwReturn["ABEMODNR"] = "010";
                    dwReturn["ABEEINSTNR"] = "000";
                    dwReturn["ABEWERTX"] = "                ";
                    dwReturn["ABEWERTN"] = "0000000000000000";
                    dwReturn["ABEPROGNR"] = "0000";
                    dwReturn["ABEEINAUS"] = " ";
                    dwReturn["ABEREZEPTKZ"] = " ";
                    dwReturn["ABEREZEPTNR"] = "          ";
                    dwReturn["ABEREZEPTST"] = "0";
                    dwReturn["ABEWERKZ"] = "              ";
                    dwReturn["ABEFAKTOR"] = "00";
                    dwReturn["ABESTAKZ"] = " ";
                    dwReturn["ABEMODEIN"] = "E";
                    dwReturn["ABEIWERTX"] = "                ";
                    dwReturn["ABEIWERTN"] = "0000000000000000";
                    dwReturn["ABENEUBEN"] = "INTEXE  ";
                    dwReturn["ABENEUDAT"] = "0";
                    dwReturn["ABEPFLKZ"] = " ";
                    dwReturn["ABEDRUCKKZ"] = " ";
                    dwReturn["ABERUEKZ"] = " ";
                    dwReturn["ABEREZPOS"] = "0000";
                    dwReturn["ABEMETHODE"] = "     ";
                    dwReturn["ABEMWERT"] = " ";
                    dwReturn["ABEMESCHL"] = "00";
                    dwReturn["ABEFILLER"] = "                  "; 
                    #endregion
                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public DataSet QueryABARTE(DataRow dwData, int option = 1)
        {
            DataSet setReturn = new DataSet();
            DataSet Result = new DataSet();
            DataTable tbLocal = null;
            OracleCommand or1 = OracleDB.CreateCommand();
            // APZMANDANT  APZART APZVAR APZANRKRS APZALFDNR APZSTUFE
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM ABARTE WHERE  ROWNUM<2 ";
                    break;
                case 1:
                    or1.CommandText = "SELECT * FROM ABARTE WHERE  "
                    + "  ABEMANDANT=:ABEMANDANT AND ABEAUFKRS=:ABEAUFKRS AND ABEAUFLFD=:ABEAUFLFD AND ABEAUFPOS=:ABEAUFPOS AND ABEAFOLGE=:ABEAFOLGE"
                    + " AND ABEAEVOR=:ABEAEVOR AND ABEMODNR=:ABEMODNR AND ABEEINSTNR=:ABEEINSTNR";
                    or1.Parameters.Add("ABEMANDANT", OracleType.NVarChar).Value = dwData["ABEMANDANT"].ToString();
                    or1.Parameters.Add("ABEAUFKRS", OracleType.NVarChar).Value = dwData["ABEAUFKRS"].ToString();
                    or1.Parameters.Add("ABEAUFLFD", OracleType.NVarChar).Value = dwData["ABEAUFLFD"].ToString();
                    or1.Parameters.Add("ABEAUFPOS", OracleType.NVarChar).Value = dwData["ABEAUFPOS"].ToString();
                    or1.Parameters.Add("ABEAFOLGE", OracleType.NVarChar).Value = dwData["ABEAFOLGE"].ToString();
                    or1.Parameters.Add("ABEAEVOR", OracleType.NVarChar).Value = dwData["ABEAEVOR"].ToString();
                    or1.Parameters.Add("ABEMODNR", OracleType.NVarChar).Value = dwData["ABEMODNR"].ToString();
                    or1.Parameters.Add("ABEEINSTNR", OracleType.NVarChar).Value = dwData["ABEEINSTNR"].ToString();
                    break;
                case 2:
                    or1.CommandText = "SELECT * FROM ABARTE WHERE  "
                    + "  ABEMANDANT=:ABEMANDANT AND ABEAUFKRS=:ABEAUFKRS AND ABEAUFLFD=:ABEAUFLFD AND ABEAUFPOS=:ABEAUFPOS";
                    or1.Parameters.Add("ABEMANDANT", OracleType.NVarChar).Value = dwData["ABEMANDANT"].ToString();
                    or1.Parameters.Add("ABEAUFKRS", OracleType.NVarChar).Value = dwData["ABEAUFKRS"].ToString();
                    or1.Parameters.Add("ABEAUFLFD", OracleType.NVarChar).Value = dwData["ABEAUFLFD"].ToString();
                    or1.Parameters.Add("ABEAUFPOS", OracleType.NVarChar).Value = dwData["ABEAUFPOS"].ToString();

                    break;
                case 3:
                    or1.CommandText = "SELECT * FROM ABARTE WHERE  "
                    + "  ABEMANDANT=:ABEMANDANT AND ABEAUFKRS=:ABEAUFKRS AND ABEAUFLFD=:ABEAUFLFD";
                    or1.Parameters.Add("ABEMANDANT", OracleType.NVarChar).Value = dwData["ABEMANDANT"].ToString();
                    or1.Parameters.Add("ABEAUFKRS", OracleType.NVarChar).Value = dwData["ABEAUFKRS"].ToString();
                    or1.Parameters.Add("ABEAUFLFD", OracleType.NVarChar).Value = dwData["ABEAUFLFD"].ToString();
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
            {
                tbLocal = Result.Tables[0];
                tbLocal.TableName = "ABARTE";
            }
            setReturn.Merge(Result);
            return setReturn;
        }
        public void SaveABARTE(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryABARTE(dwData, 1);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapABARTE(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertABARTE(dwLocal, ITXDBVERSION);
            }
            UpdateABARTE(dwLocal);
        }
        public void UpdateABARTE(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            or1.CommandText = "UPDATE ABARTE SET "
                    + "ABEMANDANT=:ABEMANDANT,"
                    + "ABEAUFKRS=:ABEAUFKRS,"
                    + "ABEAUFLFD=:ABEAUFLFD,"
                    + "ABEAUFPOS=:ABEAUFPOS,"
                    + "ABEAFOLGE=:ABEAFOLGE,"
                    + "ABEAEVOR=:ABEAEVOR,"
                    + "ABEMODNR=:ABEMODNR,"
                    + "ABEEINSTNR=:ABEEINSTNR,"
                    + "ABEWERTX=:ABEWERTX,"
                    + "ABEWERTN=:ABEWERTN,"
                    + "ABEPROGNR=:ABEPROGNR,"
                    + "ABEEINAUS=:ABEEINAUS,"
                    + "ABEREZEPTKZ=:ABEREZEPTKZ,"
                    + "ABEREZEPTNR=:ABEREZEPTNR,"
                    + "ABEREZEPTST=:ABEREZEPTST,"
                    + "ABEWERKZ=:ABEWERKZ,"
                    + "ABEFAKTOR=:ABEFAKTOR,"
                    + "ABESTAKZ=:ABESTAKZ,"
                    + "ABEMODEIN=:ABEMODEIN,"
                    + "ABEIWERTX=:ABEIWERTX,"
                    + "ABEIWERTN=:ABEIWERTN,"
                    + "ABENEUBEN=:ABENEUBEN,"
                    + "ABENEUDAT=:ABENEUDAT,"
                    + "ABEPFLKZ=:ABEPFLKZ,"
                    + "ABEDRUCKKZ=:ABEDRUCKKZ,"
                    + "ABERUEKZ=:ABERUEKZ,"
                    + "ABEREZPOS=:ABEREZPOS,"
                    + "ABEMETHODE=:ABEMETHODE,"
                    + "ABEMWERT=:ABEMWERT,"
                    + "ABEMESCHL=:ABEMESCHL,"
                    + "ABEFILLER=:ABEFILLER "
                    + " WHERE  "
                    + "  ABEMANDANT=:ABEMANDANT AND ABEAUFKRS=:ABEAUFKRS AND ABEAUFLFD=:ABEAUFLFD AND ABEAUFPOS=:ABEAUFPOS AND ABEAFOLGE=:ABEAFOLGE"
                    + "  AND ABEAEVOR=:ABEAEVOR AND ABEMODNR=:ABEMODNR AND ABEEINSTNR=:ABEEINSTNR";
            or1.Parameters.Add("ABEMANDANT", OracleType.NVarChar).Value = dwData["ABEMANDANT"].ToString();
            or1.Parameters.Add("ABEAUFKRS", OracleType.NVarChar).Value = dwData["ABEAUFKRS"].ToString();
            or1.Parameters.Add("ABEAUFLFD", OracleType.NVarChar).Value = dwData["ABEAUFLFD"].ToString();
            or1.Parameters.Add("ABEAUFPOS", OracleType.NVarChar).Value = dwData["ABEAUFPOS"].ToString();
            or1.Parameters.Add("ABEAFOLGE", OracleType.NVarChar).Value = dwData["ABEAFOLGE"].ToString();
            or1.Parameters.Add("ABEAEVOR", OracleType.NVarChar).Value = dwData["ABEAEVOR"].ToString();
            or1.Parameters.Add("ABEMODNR", OracleType.NVarChar).Value = dwData["ABEMODNR"].ToString();
            or1.Parameters.Add("ABEEINSTNR", OracleType.NVarChar).Value = dwData["ABEEINSTNR"].ToString();
            or1.Parameters.Add("ABEWERTX", OracleType.NVarChar).Value = dwData["ABEWERTX"].ToString();
            or1.Parameters.Add("ABEWERTN", OracleType.NVarChar).Value = dwData["ABEWERTN"].ToString();
            or1.Parameters.Add("ABEPROGNR", OracleType.NVarChar).Value = dwData["ABEPROGNR"].ToString();
            or1.Parameters.Add("ABEEINAUS", OracleType.NVarChar).Value = dwData["ABEEINAUS"].ToString();
            or1.Parameters.Add("ABEREZEPTKZ", OracleType.NVarChar).Value = dwData["ABEREZEPTKZ"].ToString();
            or1.Parameters.Add("ABEREZEPTNR", OracleType.NVarChar).Value = dwData["ABEREZEPTNR"].ToString();
            or1.Parameters.Add("ABEREZEPTST", OracleType.NVarChar).Value = dwData["ABEREZEPTST"].ToString();
            or1.Parameters.Add("ABEWERKZ", OracleType.NVarChar).Value = dwData["ABEWERKZ"].ToString();
            or1.Parameters.Add("ABEFAKTOR", OracleType.NVarChar).Value = dwData["ABEFAKTOR"].ToString();
            or1.Parameters.Add("ABESTAKZ", OracleType.NVarChar).Value = dwData["ABESTAKZ"].ToString();
            or1.Parameters.Add("ABEMODEIN", OracleType.NVarChar).Value = dwData["ABEMODEIN"].ToString();
            or1.Parameters.Add("ABEIWERTX", OracleType.NVarChar).Value = dwData["ABEIWERTX"].ToString();
            or1.Parameters.Add("ABEIWERTN", OracleType.NVarChar).Value = dwData["ABEIWERTN"].ToString();
            or1.Parameters.Add("ABENEUBEN", OracleType.NVarChar).Value = dwData["ABENEUBEN"].ToString();
            or1.Parameters.Add("ABENEUDAT", OracleType.NVarChar).Value = dwData["ABENEUDAT"].ToString();
            or1.Parameters.Add("ABEPFLKZ", OracleType.NVarChar).Value = dwData["ABEPFLKZ"].ToString();
            or1.Parameters.Add("ABEDRUCKKZ", OracleType.NVarChar).Value = dwData["ABEDRUCKKZ"].ToString();
            or1.Parameters.Add("ABERUEKZ", OracleType.NVarChar).Value = dwData["ABERUEKZ"].ToString();
            or1.Parameters.Add("ABEREZPOS", OracleType.NVarChar).Value = dwData["ABEREZPOS"].ToString();
            or1.Parameters.Add("ABEMETHODE", OracleType.NVarChar).Value = dwData["ABEMETHODE"].ToString();
            or1.Parameters.Add("ABEMWERT", OracleType.NVarChar).Value = dwData["ABEMWERT"].ToString();
            or1.Parameters.Add("ABEMESCHL", OracleType.NVarChar).Value = dwData["ABEMESCHL"].ToString();
            or1.Parameters.Add("ABEFILLER", OracleType.NVarChar).Value = dwData["ABEFILLER"].ToString();
            Connect2Proxy(or1);
        }
        public void MapABARTE(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void InsertABARTE(DataRow dwData, string ITXDBVERSION = "50.24")
        {

            or1 = OracleDB.CreateCommand();
            or1.CommandText = "INSERT INTO ABARTE(ABEMANDANT, ABEAUFKRS, ABEAUFLFD, ABEAUFPOS, ABEAFOLGE, ABEAEVOR, ABEMODNR, ABEEINSTNR, ABEWERTX, ABEWERTN, ABEPROGNR, ABEEINAUS, ABEREZEPTKZ, ABEREZEPTNR, ABEREZEPTST, ABEWERKZ, ABEFAKTOR, ABESTAKZ, ABEMODEIN, ABEIWERTX, ABEIWERTN, ABENEUBEN, ABENEUDAT, ABEPFLKZ, ABEDRUCKKZ, ABERUEKZ, ABEREZPOS, ABEMETHODE, ABEMWERT, ABEMESCHL, ABEFILLER)"
                + " VALUES (:ABEMANDANT, :ABEAUFKRS, :ABEAUFLFD, :ABEAUFPOS, :ABEAFOLGE, :ABEAEVOR, :ABEMODNR, :ABEEINSTNR, '16              ', '0000000016000000', '0000', ' ', ' ', '          ', '0', '              ', '00', ' ', ' ', '                ', '0000000000000000', 'INTEXE  ', 20140401, 'N', 'J', 'N', '0000', '     ', ' ', '00', '                  ')";
            or1.Parameters.Add("ABEMANDANT", OracleType.NVarChar).Value = dwData["ABEMANDANT"].ToString();
            or1.Parameters.Add("ABEAUFKRS", OracleType.NVarChar).Value = dwData["ABEAUFKRS"].ToString();
            or1.Parameters.Add("ABEAUFLFD", OracleType.NVarChar).Value = dwData["ABEAUFLFD"].ToString();
            or1.Parameters.Add("ABEAUFPOS", OracleType.NVarChar).Value = dwData["ABEAUFPOS"].ToString();
            or1.Parameters.Add("ABEAFOLGE", OracleType.NVarChar).Value = dwData["ABEAFOLGE"].ToString();
            or1.Parameters.Add("ABEAEVOR", OracleType.NVarChar).Value = dwData["ABEAEVOR"].ToString();
            or1.Parameters.Add("ABEMODNR", OracleType.NVarChar).Value = dwData["ABEMODNR"].ToString();
            or1.Parameters.Add("ABEEINSTNR", OracleType.NVarChar).Value = dwData["ABEEINSTNR"].ToString();
            Connect2Proxy(or1);

        }
        public void DeleteABARTE(string client, string krs, string lfd, string pos, int option = 1)
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM ABARTE WHERE  "
               + "  ABEMANDANT=:ABEMANDANT AND ABEAUFKRS=:ABEAUFKRS AND ABEAUFLFD=:ABEAUFLFD AND ABEAUFPOS=:ABEAUFPOS ";
                    or1.Parameters.Add("ABEMANDANT", OracleType.NVarChar).Value = client;
                    or1.Parameters.Add("ABEAUFKRS", OracleType.NVarChar).Value = krs;
                    or1.Parameters.Add("ABEAUFLFD", OracleType.NVarChar).Value = lfd;
                    or1.Parameters.Add("ABEAUFPOS", OracleType.NVarChar).Value = pos;
                    break;
                case 2:
                    or1.CommandText = "DELETE FROM ABARTE WHERE  "
               + "  ABEMANDANT=:ABEMANDANT AND ABEAUFKRS=:ABEAUFKRS AND ABEAUFLFD=:ABEAUFLFD ";
                    or1.Parameters.Add("ABEMANDANT", OracleType.NVarChar).Value = client;
                    or1.Parameters.Add("ABEAUFKRS", OracleType.NVarChar).Value = krs;
                    or1.Parameters.Add("ABEAUFLFD", OracleType.NVarChar).Value = lfd;
                    
                    break;
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
